package com.example.distillapplication.ui.fragment.set;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.distillapplication.R;
import com.example.distillapplication.databinding.FragmentSetTimeBinding;
import com.example.distillapplication.ui.fragment.base.BaseFragment;
import com.example.distillapplication.hardware.protcal.LogString;

/**
 * A simple {@link Fragment} subclass.
 */
public class SetTimeFragment extends BaseFragment {
    FragmentSetTimeBinding fragmentSetTimeBinding;
    ISetTimeAndSteamVolume setTimeAndSteamVolume;

    public void setSetTimeAndSteamVolume(ISetTimeAndSteamVolume setTimeAndSteamVolume) {
        this.setTimeAndSteamVolume = setTimeAndSteamVolume;
    }

    public SetTimeFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_set_time, container, false);
        fragmentSetTimeBinding = FragmentSetTimeBinding.bind(view);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        setSetTimeAndSteamVolume((ISetTimeAndSteamVolume) mActivity);
        if(mSerialMessageViewModel.onDistillFlag.getValue() == 1){
            fragmentSetTimeBinding.clockView.setCurTime(mInstrumentStateViewModel.targetTime.getValue());
        }


        fragmentSetTimeBinding.plus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int volume;
                if(fragmentSetTimeBinding.time.getText().toString().equals("")){
                    volume = 1;
                    fragmentSetTimeBinding.time.setText(String.valueOf(volume));
                    return;
                }
                volume = Integer.valueOf(fragmentSetTimeBinding.time.getText().toString());
                volume ++ ;
                fragmentSetTimeBinding.time.setText(String.valueOf(volume));
            }
        });

        fragmentSetTimeBinding.minus.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int volume;
                if(fragmentSetTimeBinding.time.getText().toString().equals("") || fragmentSetTimeBinding.time.getText().toString().equals("0")){
                    return;
                }
                volume = Integer.valueOf(fragmentSetTimeBinding.time.getText().toString());
                volume -- ;
                fragmentSetTimeBinding.time.setText(String.valueOf(volume));
            }
        });

        fragmentSetTimeBinding.setup.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String time = fragmentSetTimeBinding.time.getText().toString();
                if(time.equals("")){
                    showLongToast("请输入蒸馏时间！");
                }else {
                    mInstrumentStateViewModel.targetTime.postValue(Integer.valueOf(time));
                    fragmentSetTimeBinding.clockView.antiClockwise(Integer.valueOf(time));
                    mSerialMessageViewModel.timeSetup.postValue(true);
                    mSharedViewModel.setOperationData(LogString.setTime + time + "s\n");
                }

            }
        });

        fragmentSetTimeBinding.start.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mSerialMessageViewModel.onDistillFlag.getValue() != 0){
                    showLongToast("当前正在蒸馏过程中，请等待蒸馏完毕！");
                }else if(!mSerialMessageViewModel.timeSetup.getValue()){
                    showLongToast("请先设置蒸馏时间！");
                }else {
                    String time = fragmentSetTimeBinding.time.getText().toString();
                    mSerialMessageViewModel.onDistillFlag.postValue(1);//时间模式
                    setTimeAndSteamVolume.setTime(Integer.valueOf(time));
                    mSharedViewModel.setOperationData(LogString.start);
                }
            }
        });

        mInstrumentStateViewModel.targetTime.observe(getViewLifecycleOwner(), new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                fragmentSetTimeBinding.clockView.setCurTime(integer);
            }
        });

        mSerialMessageViewModel.onDistillFlag.observe(getViewLifecycleOwner(), new Observer<Integer>() {
            @Override
            public void onChanged(Integer integer) {
                if(integer == 0){
                    fragmentSetTimeBinding.time.setText("");
                }
            }
        });
    }
}
